{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 多变量的线性方程\n",
    "以波士顿房价预测为例\n",
    "波士顿房价数据集包括506个样本，每个样本包括12个特征变量和该地区的平均房价"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             CRIM         ZN       INDUS         CHAS         NOX          RM  \\\n",
      "count  506.000000  506.000000  506.000000  506.000000  506.000000  506.000000   \n",
      "mean     3.613524   11.363636   11.136779    0.069170    0.554695    6.284634   \n",
      "std      8.601545   23.322453    6.860353    0.253994    0.115878    0.702617   \n",
      "min      0.006320    0.000000    0.460000    0.000000    0.385000    3.561000   \n",
      "25%      0.082045    0.000000    5.190000    0.000000    0.449000    5.885500   \n",
      "50%      0.256510    0.000000    9.690000    0.000000    0.538000    6.208500   \n",
      "75%      3.677082   12.500000   18.100000    0.000000    0.624000    6.623500   \n",
      "max     88.976200  100.000000   27.740000    1.000000    0.871000    8.780000   \n",
      "\n",
      "              AGE         DIS         RAD         TAX     PTRATIO       LSTAT  \\\n",
      "count  506.000000  506.000000  506.000000  506.000000  506.000000  506.000000   \n",
      "mean    68.574901    3.795043    9.549407  408.237154   18.455534   12.653063   \n",
      "std     28.148861    2.105710    8.707259  168.537116    2.164946    7.141062   \n",
      "min      2.900000    1.129600    1.000000  187.000000   12.600000    1.730000   \n",
      "25%     45.025000    2.100175    4.000000  279.000000   17.400000    6.950000   \n",
      "50%     77.500000    3.207450    5.000000  330.000000   19.050000   11.360000   \n",
      "75%     94.075000    5.188425   24.000000  666.000000   20.200000   16.955000   \n",
      "max    100.000000   12.126500   24.000000  711.000000   22.000000   37.970000   \n",
      "\n",
      "             MEDV  \n",
      "count  506.000000  \n",
      "mean    22.532806  \n",
      "std      9.197104  \n",
      "min      5.000000  \n",
      "25%     17.025000  \n",
      "50%     21.200000  \n",
      "75%     25.000000  \n",
      "max     50.000000  \n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from sklearn.utils import shuffle\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "# 读取数据文件,并且第一行为数据的开头\n",
    "df = pd.read_csv(\"../data/boston housing price.csv\", header=0)\n",
    "\n",
    "# 显示数据摘要描述信息\n",
    "print(df.describe())\n",
    "\n",
    "df = np.array(df.values)  # 获取df的值并且转换成 np 的数组格式\n",
    "\n",
    "y_data = df[:, 12]  # 标签数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 特征缩放\n",
    "归一化，将特征缩放在0-1之间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 归一化\n",
    "for i in range(12):\n",
    "    df[:, i] = (df[:, i] - df[:, i].min()) / (df[:, i].max() - df[:, i].min())\n",
    "x_data = df[:, :12]  # 特征数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 构建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    }
   ],
   "source": [
    "# 定义训练数据的占位符， x是特征值， y是标签值\n",
    "x = tf.placeholder(tf.float32, [None, 12], name=\"X\")\n",
    "y = tf.placeholder(tf.float32, [None, 1], name=\"Y\")\n",
    "\n",
    "# 创建一个命名空间，定义模型函数 y = w1 * x1 + ... + w12 * x12+b\n",
    "with tf.name_scope(\"Model1\"):\n",
    "    # 初始化w为shape=（12,1），服从标准差为0.01的随机正态分布的数\n",
    "    w = tf.Variable(tf.random_normal([12, 1], stddev=0.01), name=\"W\")\n",
    "    # 初始化b为1.0\n",
    "    b = tf.Variable(1.0, name=\"b\")\n",
    "\n",
    "\n",
    "    # 定义模型函数 y = W * X+ b 矩阵相乘matmul\n",
    "    def model(x, w, b):\n",
    "        return tf.matmul(x, w) + b\n",
    "\n",
    "\n",
    "    # 定义线性函数的预测值\n",
    "    pred = model(x, w, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xt4XPV95/H3V9LMSBqNJEsayfLdxgZDCDZEcUigJJg0Sy4ltE1SsklLGhLafdpN2qRNQvbZbdMt2WTbFLq9ZJeGBNoNbVISSpZSGgImCU0DyGDAjq8Y2/iimy3rfpuZ7/4xR8bYkjW2NBrPmc/reeaZmaMzM9/zePzRT7/z+/2OuTsiIlL8ygpdgIiIzA0FuohISCjQRURCQoEuIhISCnQRkZBQoIuIhIQCXUQkJBToIiIhoUAXEQmJivn8sKamJl+xYsV8fqSISNHbvHlzj7snZ9pvXgN9xYoVtLe3z+dHiogUPTPbn8t+6nIREQkJBbqISEgo0EVEQkKBLiISEgp0EZGQUKCLiISEAl1EJCSKItAf39HJXz+xp9BliIic13IKdDOrN7P7zWyHmW03szebWYOZPWpmu4P7Bfkq8se7e/jqppfy9fYiIqGQawv9z4FH3H0tsA7YDnwOeMzd1wCPBc/zojEeZW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x92eDxwNk/5MvJuTH7lmDwdNIcHNgI3B/sD10xw1gZkuAdwNfC54bJXDc05iz73kxBPpi4JWTnh8MtpWKFnc/AtngA5oLXE9emdkK4HLgKUrg2INuhy1AF/Ao8BJw3N1TwS5h/b7fCXwGyATPGymN43bg+2a22cxuDbbN2fe8GK4pOtXVdTU0J4TMrAb4DvA77t6fbbSFm7ungfVmVg88AFw81W7zW1V+mdl7gC5332xmb5vcPMWuoTruwFXuftjMmoFHzWzHXL55MbTQDwJLT3q+BDhcoFoKodPMWgGC+64C15MXZhYhG+bfdPfvBptL4tgB3P048ATZcwj1ZjbZ2Arj9/0q4AYz20e2C3Uj2RZ72I8bdz8c3HeR/QW+gTn8nhdDoD8DrAnOgEeBm4DvFbim+fQ94Obg8c3AgwWsJS+C/tO7ge3u/mcn/SjUx25myaBljplVAW8ne/5gE/C+YLfQHbe73+buS9x9Bdn/z4+7+4cI+XGbWdzMEpOPgXcAW5nD73lRTCwys3eR/Q1eDnzd3W8vcEl5YWZ/D7yN7OprncAfAP8EfBtYBhwA3u/up544LWpmdjXwY+BFXu1T/TzZfvTQHruZXUb2JFg52cbVt939j8xsFdlZYQ0CAAAAY0lEQVSWawPwHPBhdx8rXKX5E3S5/J67vyfsxx0c3wPB0wrgPne/3cwamaPveVEEuoiIzKwYulxERCQHCnQRkZBQoIuIhIQCXUQkJBToIiIhoUAXEQkJBbqISEgo0EVEQuL/AzEUBJ/gH1BzAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_epochs = 50  # 迭代次数\n",
    "learning_rate = 0.01  # 学习率\n",
    "\n",
    "# 创建一个命名空间，定义损失函数,采用均方差作为损失函数\n",
    "with tf.name_scope(\"LossFunction\"):\n",
    "    loss_function = tf.reduce_mean(tf.square(y - pred))\n",
    "\n",
    "# 梯度下降优化器 设置学习率和优化目标损失最小化\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)\n",
    "\n",
    "sess = tf.Session()  # 建立会话\n",
    "init = tf.global_variables_initializer()  # 变量初始化\n",
    "sess.run(init)\n",
    "\n",
    "loss_list = []  # 用于保存loss的值\n",
    "# 迭代训练\n",
    "for epoch in range(train_epochs):\n",
    "    loss_sum = 0.0\n",
    "    for xs, ys in zip(x_data, y_data):\n",
    "        xs = xs.reshape(1, 12)\n",
    "        ys = ys.reshape(1, 1)\n",
    "\n",
    "        _, loss = sess.run([optimizer, loss_function], feed_dict={x: xs, y: ys})\n",
    "\n",
    "        loss_sum = loss_sum + loss  # 累加损失\n",
    "\n",
    "    x_data, y_data = shuffle(x_data, y_data)  # 打乱数据顺序 避免过拟合假性学习\n",
    "\n",
    "    b0temp = b.eval(session=sess)\n",
    "    w0temp = w.eval(session=sess)\n",
    "    loss_average = loss_sum / len(y_data)  # 所有数据的平均损失\n",
    "    loss_list.append(loss_average)\n",
    "plt.plot(loss_list)  # 显示迭代过程中的平均代价\n",
    "plt.show()  # 显示图表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 模型预测\n",
    "随机抽取数据验证："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值：14.972874\n",
      "目标值：15.100000\n"
     ]
    }
   ],
   "source": [
    "# 随机抽取数据验证\n",
    "n = np.random.randint(506)\n",
    "x_test = x_data[n]\n",
    "x_test = x_test.reshape(1, 12)\n",
    "\n",
    "predict = sess.run(pred, feed_dict={x: x_test})\n",
    "print(\"预测值：%f\" % predict)\n",
    "\n",
    "target = y_data[n]\n",
    "print(\"目标值：%f\" % target)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
